package com.example.ivideo_2211a.commodity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.ivideo_2211a.R
import retrofit2.http.Path

@Route(path = "/app/CustomerServiceActivity")
class CustomerServiceActivity : AppCompatActivity() {
    private val messages = mutableListOf<Pair<String, Boolean>>() // Pair<内容, 是否用户>
    private lateinit var adapter: MessageAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customer_service)


        val lvMessages = findViewById<android.widget.ListView>(R.id.lv_messages)
        val etInput = findViewById<android.widget.EditText>(R.id.et_input)
        val btnSend = findViewById<android.widget.Button>(R.id.btn_send)
        val ivback = findViewById<android.widget.ImageView>(R.id.back)

        adapter = MessageAdapter(this, messages)
        lvMessages.adapter = adapter

        ivback.setOnClickListener {
            finish()
        }

        // 欢迎语
        messages.add(Pair("您好，请问有什么可以帮您？", false))
        adapter.notifyDataSetChanged()

        btnSend.setOnClickListener {
            val content = etInput.text.toString().trim()
            if (content.isNotEmpty()) {
                messages.add(Pair(content, true))
                adapter.notifyDataSetChanged()
                etInput.setText("")
                lvMessages.smoothScrollToPosition(messages.size - 1)
                // 智能自动回复
                lvMessages.postDelayed({
                    val reply = getAutoReply(content)
                    messages.add(Pair(reply, false))
                    adapter.notifyDataSetChanged()
                    lvMessages.smoothScrollToPosition(messages.size - 1)
                }, 800)
            }
        }



    }

    // 简单的自动回复逻辑
    private fun getAutoReply(question: String): String {
        val q = question.lowercase()
        return when {
            q.contains("你好") || q.contains("您好") -> "您好，有什么可以帮您？"
            q.contains("价格") || q.contains("多少钱") -> "商品价格请以页面显示为准，如有疑问可咨询人工客服。"
            q.contains("发货") -> "我们会在下单后24小时内发货，节假日顺延。"
            q.contains("快递") || q.contains("物流") -> "商品发出后可在订单详情中查看物流信息。"
            q.contains("售后") || q.contains("退货") -> "如需售后或退货，请在订单详情中申请，或联系客服协助。"
            q.contains("发票") -> "如需发票，请下单时备注或联系客服提供开票信息。"
            q.contains("人工") || q.contains("转接") -> "已为您转接人工客服，请稍候..."
            else -> "客服：我们已收到您的消息，会尽快回复您。"
        }
    }
    }

